<?php
/**
 * Author:xsc
 * Time:2023/9/20 15:49
 */

namespace App\Repository;

use App\Models\GoodsLog;

class GoodsLogRepo extends BaseRepo
{

    /**
     * 新增/更新记录
     * @param array $data
     * @param int $id
     * @return int
     * Author:xsc
     * Time:2023/9/20 15:51
     */
    public function save(array $data, int $id = 0)
    {
        if ($id) {
            return GoodsLog::query()->where('id', $id)->update($data);
        } else {
            return GoodsLog::query()->insertGetId($data);
        }
    }

    /**
     * 记录详情
     * @param int $goods_id
     * @param int $from_member_id
     * @param int $to_member_id
     * @param int $type
     * @return array
     * Author:xsc
     * Time:2023/10/7 9:53
     */
    public function detail(int $goods_id, int $from_member_id = 0, int $to_member_id = 0, int $type = 10)
    {
        $where = [
            ['goods_id', '=', $goods_id],
        ];
        if ($from_member_id) {
            $where[] = ['from_member_id', '=', $from_member_id];
        }
        if ($to_member_id) {
            $where[] = ['to_member_id', '=', $to_member_id];
        }
        if ($type) {
            $where[] = ['type', '=', $type];
        }
        $res = GoodsLog::query()->where($where)->orderBy('id', 'desc')->first();
        return $res ? $res->toArray() : [];
    }

}
